SWEP.Author = "Meoowe/GParkour creators"
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = "Jump first before starting to climb.\nUse primary fire to climb.\nUse secondary fire to grab onto a ledge when near one.\nSecondary attack while grabbed onto a ledge - let go of the ledge.\nPrimary attack while grabbed onto a ledge - pull yourself up."

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = "models/weapons/v_hands.mdl"
SWEP.WorldModel = ""
SWEP.HoldType = "normal"

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"

SWEP.Delay = 0.25
SWEP.UsedTimes = 0
SWEP.Hanging = false
SWEP.InitialPower = 240
SWEP.VelocityMinus = 15

function SWEP:DrawWorldModel()
	return false
end

function SWEP:Initialize()
	self:SetWeaponHoldType(self.HoldType)
	self.Weapon:DrawShadow(false)
	
	self.ClimbSound = {
	Sound("physics/body/body_medium_impact_soft1.wav"),
	Sound("physics/body/body_medium_impact_soft2.wav"),
	Sound("physics/body/body_medium_impact_soft3.wav"),
	Sound("physics/body/body_medium_impact_soft4.wav") }
	
	self.GrabSound = {
	Sound("physics/flesh/flesh_impact_hard1.wav"),
	Sound("physics/flesh/flesh_impact_hard2.wav"),
	Sound("physics/flesh/flesh_impact_hard4.wav"),
	Sound("physics/flesh/flesh_impact_hard5.wav") }
	
	self.BreakNeck = {
	Sound("physics/body/body_medium_break2.wav"),
	Sound("physics/body/body_medium_break3.wav"),
	Sound("physics/body/body_medium_break4.wav") }
	
	self.SwingSound = {
	Sound("npc/fast_zombie/claw_miss1.wav"),
	Sound("npc/fast_zombie/claw_miss2.wav") }
	
	self.HitMan = {
	Sound("npc/vort/foot_hit.wav"),
	Sound("npc/zombie/zombie_hit.wav") }
end

// Reload

function SWEP:Reload()
end

// Think

function SWEP:Think()
	if CLIENT then return end
	if self.Owner:OnGround() then
		self.UsedTimes = 0
		self.Hanging = false
	end
	
	if self.Owner:KeyPressed(IN_JUMP) then
		if not self.Owner:OnGround() then
			if self.Hanging == false and not self.Weapon:CanGrab() then
				if self.UsedTimes == 2 then
					local trace = self.Owner:GetEyeTrace()
					if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 35 then
						if SERVER then
							self.Owner:SetLocalVelocity(self.Owner:GetForward() * -375 + Vector(0, 0, 50))
							self.Owner:SetEyeAngles(self.Owner:EyeAngles() - Vector(0, 180, 0))
						end
						self.UsedTimes = 3
						self.Owner:ViewPunch(Angle(-10, 0, 0))
					end
				end
			end
		end
	end
	
	if self.HasReleased == false and not self.Owner:KeyDown(IN_ATTACK2) then
		self.HasReleased = true
	end
end


function SWEP:PrimaryAttack()
	if CLIENT then return end
	if not self.Owner:OnGround() then
		if self.Hanging == false or not self.Owner:GetMoveType() == MOVETYPE_NONE then
			local pos = self.Owner:GetShootPos()
			local ang = self.Owner:GetAimVector()
			if self.UsedTimes < 3 then
				local tracedata = {}
				tracedata.start = pos
				tracedata.endpos = pos+(ang*25)
				tracedata.filter = self.Owner
				local trace = util.TraceLine(tracedata)
				if ( trace.HitWorld or trace.Entity:IsValid() and not trace.Entity:IsPlayer() ) then
					local Vel = self.Owner:GetVelocity()
					if SERVER then
						self.Owner:SetVelocity(Vector(0, 0, self.InitialPower - (self.UsedTimes * self.VelocityMinus) - Vel.z))
					end
					self.Owner:EmitSound(self.ClimbSound[math.random(#self.ClimbSound)], 75, math.random(95, 105))
					self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
					self.UsedTimes = self.UsedTimes + 1
					self.Weapon:ShakeEffect()
				end
			end
		else
		local Vel = self.Owner:GetVelocity()
			if SERVER then
				self.Owner:SetMoveType(MOVETYPE_WALK)
				self.Owner:SetLocalVelocity(Vector(0, 0, 250))
			end
			self.Owner:EmitSound(self.ClimbSound[math.random(#self.ClimbSound)], 75, math.random(95, 105))
			self.Owner:EmitSound("player/suit_sprint.wav", 85, math.random(95, 105))
			timer.Simple(0.3, function() 
				self.Hanging = false
			end)
			self.Weapon:SetNextPrimaryFire( CurTime() + (self.Delay + 0.15) )
			self.Owner:ViewPunch(Angle(-7, 0, 0))
		end	
	end
end

function SWEP:ShakeEffect()
	if self.UsedTimes == 1 then
		self.Owner:ViewPunch(Angle(0, 5, 0))
	elseif self.UsedTimes == 2 then
		self.Owner:ViewPunch(Angle(0, -5, 0))
	elseif self.UsedTimes == 3 then
		self.Owner:ViewPunch(Angle(-5, 0, 0))
	end
end

// SecondaryAttack

function SWEP:CanGrab() -- this part is not by me, it's by guys who made GParkour
	local trace = {}
	trace.start = self.Owner:GetShootPos() + Vector( 0, 0, 15 )
	trace.endpos = trace.start + (self.Owner:GetAimVector():Angle():Forward( ) * 30)
	trace.filter = self.Owner

	local trHi = util.TraceLine(trace)
	
	local trace = {}
	trace.start = self.Owner:GetShootPos()
	trace.endpos = trace.start + (self.Owner:GetAimVector():Angle():Forward( ) * 30)
	trace.filter = self.Owner

	local trLo = util.TraceLine(trace)
	
	if trLo and trHi and trLo.HitWorld  and !trHi.Hit then
		return true
	else 
		return false
	end
end -- And everything below is by me

function SWEP:SecondaryAttack()
	if CLIENT then return end
	if self.HasReleased == false then 
		return -- We don't want to let go of the ledge if we were accidently holding the secondary attack button while we were hanging on it, right?
	end
	
	local trace = self.Owner:GetEyeTrace()
	if not self.Owner:OnGround() then
		if self.Owner:GetVelocity().z <= 900 then
			if self.Hanging == false and self.Weapon:CanGrab() then
				if SERVER then
					self.Owner:SetMoveType(MOVETYPE_NONE)
				end
				self.Hanging = true
				self.HasReleased = false -- We don't want to let go of the ledge if we were accidently holding the secondary attack button while we were hanging on it, right?
				self.Owner:EmitSound(self.GrabSound[math.random(#self.GrabSound)], 75, math.random(95, 105))
				self.Weapon:SetNextSecondaryFire(CurTime() + 0.35)
				self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
				if self.Owner:GetVelocity().z < 0 then
					self.Owner:ViewPunch(Angle(10, 0, 0))
				else
					self.Owner:ViewPunch(Angle(-5, 0, 0))
				end
			elseif self.Hanging == true then
				local Vel = self.Owner:GetVelocity()
				if SERVER then
					self.Owner:SetMoveType(MOVETYPE_WALK)
					self.Owner:SetLocalVelocity(Vector(0, 0, 0))
				end
				self.Hanging = false
				self.UsedTimes = 3
				self.Weapon:SetNextSecondaryFire(CurTime() + 0.35)
				self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
			end	
		end
	end
end 
